﻿package domino
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	import mochi.as3.*;
	
	
	/**
	 * ...
	 * @author sergiy
	 */
	public dynamic class Preloader extends MovieClip 
	{
		[Embed(source="img/bg.svg")]
           public var bg:Class;
		   
		
		   
		   
		private var progress2:TextField;  
		private	var bg2:Sprite; 
		private var loader2:Sprite = new loader();
		private var title2:TextField;//= new title();
		private var format1:TextFormat = new TextFormat();
		public function Preloader() 
		{
			init();
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			//loaderInfo.
			// show loader
		}//end
		private function init():void {
			
//MochiAd.showPreGameAd({clip:root, id:"e8f1fb7c28f77337", res:"640x480"});
		    progress2 = new TextField();
			progress2.x = 300;
			progress2.y = 60;
			progress2.mouseEnabled = false;

			format1.font="Hobo Std";
            format1.color = 0x996600;
			format1.bold = true;
			format1.size = 22;
			title2 = new TextField();
			
		   
			
			bg2= new bg(); 
			addChild(bg2);
			
			loader2.x = 240;
			loader2.y = 180;
			loader2.mouseEnabled = false;
			addChild(loader2);
		
			
			//addChild(progress2);
			}//end
			
			private function onConnectError():void
			{
				trace("error");
			}
		
		private function progress(e:ProgressEvent):void 
		{
			// update loader
			progress2.text = String(Math.round(100 * (e.bytesLoaded / e.bytesTotal))) + "%";
			//trace(e.bytesLoaded);
			//trace(e.bytesTotal);
			//trace("loading");
			
		}//end
		
		private function checkFrame(e:Event):void 
		{
			trace(currentFrame);
			trace(totalFrames);
			if (currentFrame == totalFrames) 
			{
				removeEventListener(Event.ENTER_FRAME, checkFrame);
				startup();
			}
		}//end
		
		private function startup():void 
		{
			// hide loader
			stop();
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			var mainClass:Class = getDefinitionByName("domino.Main") as Class;
			removeChild(loader2);
			//removeChild(progress2);
			removeChild(bg2);
			//removeChild(title2);
			addChild(new mainClass() as DisplayObject);
			
		}//end
		
	}
	
}